package com.example.nophase.custom;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.nophase.R;

public class CustomAlertDialog extends Dialog {

    private Context context;
    private String title = "";
    private String message = "";
    private String confirmBtnText = "";

    private CustomAlertDialog.ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        void doConfirm();

    }

    public CustomAlertDialog(Context context, String title, String message) {
        super(context, R.style.customDialog);
        this.context = context;
        this.title = title;
        this.message = message;
    }

    public CustomAlertDialog(Context context, String title, String message, String confirmBtnText) {
        this(context,title,message);
        this.confirmBtnText = confirmBtnText;

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.alert_dialog_layout, null);
        setContentView(view);

        TextView tvTitle = view.findViewById(R.id.my_dialog_title);
        TextView tvMessage = view.findViewById(R.id.my_dialog_content);
        Button btnConfirm =  view.findViewById(R.id.btn_main_activity_comfirm);

        tvTitle.setText(title);
        tvMessage.setText(message);
        if(! confirmBtnText.isEmpty()) {
            btnConfirm.setText(confirmBtnText);

        }

        btnConfirm.setOnClickListener(new CustomAlertDialog.CustomDialogClickListener());


        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }
    public void setClicklistener(CustomAlertDialog.ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class CustomDialogClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.btn_main_activity_comfirm:
                    clickListenerInterface.doConfirm();
                    break;

            }
        }
    };
}
